package com.xywy.bloodandroid.act;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.adapter.OnLineMessageAdapter;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.OnlineQuestionList;
import com.xywy.bloodandroid.bean.SystemMessageData;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.util.JsonUtil;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
import com.xywy.bloodandroid.vollery.PostRequest;

/**
 * 系统消息
 */
public class SystemMessageActivity extends BaseActivity {
	// private SystemMessageAdapter adatper;
	private RelativeLayout left_img;// 标题返回按钮
	private ListView lv_message; // 系统消息展示；
	private TextView middle_title; // 标题
	private OnLineMessageAdapter adapter;
	private List<OnlineQuestionList> list = new ArrayList<OnlineQuestionList>();
	private List<SystemMessageData> systemMessage = new ArrayList<SystemMessageData>();
	private List<SystemMessageData> temp = new ArrayList<SystemMessageData>();
	private SharedPreferences sp;
	private Editor editor;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_system_message);
		lv_message = (ListView) findViewById(R.id.lv_message);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("系统消息");
		left_img = (RelativeLayout) findViewById(R.id.left_img);

	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			this.finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected void initData() {
		getServerData();
	}

	/**
	 * 从服务器获取当前设备数据
	 */
	private void getServerData() {
		// http://api.yun.xywy.com/index.php/glu_app/ad/msg_box/
		String url = "http://api.yun.xywy.com/index.php/glu_app/ad/msg_box/";
		PostRequest<String> postRequest = new PostRequest<String>(url,
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						UIUtil.showToast(context, context.getResources()
								.getString(R.string.network_slow));
					}

					@Override
					public void onSuccess(String response) {
						UIUtil.DissDialog(context, waittingDialog);

						try {
							ParserData(response);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		long lastTime = sp.getLong("system_message_last_save", 1);
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("xywy_userid", User.getInstance(context).getUserid());// userID
		params.put("datatime", lastTime + "");// 最后一条时间
		params.put("fromurl", "app_tang");// 最后一条时间
		params.put("type", 1 + "");// password
		postRequest.setParams(params);
		postRequest.setIsParseJson(false);
		executeRequest(postRequest);

	}

	/**
	 * 解析当前从服务器上获取的数据
	 * 
	 * @param response
	 *            获取的String
	 * @throws JSONException
	 *             json解析异常
	 */
	private void ParserData(String response) throws JSONException {
		JSONObject jsonObject = new JSONObject(response);
		int state = jsonObject.getInt("state");
		if (state == 200) { // 请求成功
			// 更新最后一次拉取的时间
			if (state == 200) {
				if (jsonObject.has("data")
						&& jsonObject.getString("data").length() > 0) {
					list.addAll((List<OnlineQuestionList>) JsonUtil
							.parseJsonToList(jsonObject.getString("data"),
									new TypeToken<List<OnlineQuestionList>>() {
									}.getType()));
					adapter = new OnLineMessageAdapter(
							SystemMessageActivity.this, list);
					lv_message.setAdapter(adapter);
					if (list == null || list.size() < 1) {
						showToast("当前没有系统信息");
					}
				}
			} else {
				// TODO: 此处展示无数据位置
				UIUtil.showToast(context, jsonObject.getString("msg"));
			}

		}
	}

}
